package cn.itcast.jx.action.sysadmin;

import java.util.List;
import java.util.ListIterator;

import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.common.SysConstant;
import cn.itcast.jx.domain.Dept;
import cn.itcast.jx.service.IDeptService;
import cn.itcast.jx.util.Page;

public class DeptAction extends BaseAction  implements ModelDriven<Dept>{

	private IDeptService deptService;
	
	private String menuType = "dept";

	public String list() {
		String hql="from Dept";
		deptService.findPage(hql, page, Dept.class,null);
		//设置分页组件的url
		page.setUrl("deptAction_list");//相对定位
		push(page);
		return SUCCESS;
	}
	public String toview() {
		String hql = "from Dept";
		Dept dept = deptService.get(Dept.class, model.getId());
		push(dept);
		return "view";
	}
	public String tocreate() {
		String hql = "from Dept where state=1";
		List<Dept> deptList = deptService.find(hql, Dept.class, null);
		put("deptList",deptList);
		return "create";
	}
	
	public String insert() {
		model.setState(SysConstant.ENABLED);
		deptService.saveOrUpdate(model);
		return "list";
	}

	public String delete() {
		String[] ids = model.getId().split(", ");
		
		deptService.delete(ids);
		
		return "list";
	}
	public String toupdate() {
		String hql = "from Dept t where t.state=1";
		List<Dept> deptList1 = deptService.find(hql, Dept.class, null);
		
		Dept dept = deptService.get(Dept.class, model.getId());

		List<Dept> deptList = deptService.removeDept(deptList1, dept);
		
		put("deptList", deptList);
		push(dept);
		return "update";
	}
	public String update() {
		deptService.saveOrUpdate(model);
		return "list";
	}
	
	public void setDeptService(IDeptService deptService) {
		this.deptService = deptService;
	}

	@Override
	public Dept getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	//分页组件
	private Page page =new Page();


	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	public String getMenuType() {
		return menuType;
	}
	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}
	private Dept model = new Dept();
	
}
